<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<div class="container-fluid am-margin">
    <form id="orgForm">
        <input type="hidden" name="id"/>
        <input type="hidden" name="pid"/>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="name">名称</label>
                        <input type="text" name="name" class="form-control" check-type="required">
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="fullName">全称</label>
                        <input type="text" name="fullName" class="form-control" >
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="code">编码</label>
                        <input type="text" name="code" class="form-control" >
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="name">类型</label>
                        <select name="orgType" class="form-control">
                            <pf:dataDic var="item" dicCode="ORG_TYPE">
                                <option value="${item.value}">${item.name}</option>
                            </pf:dataDic>
                        </select>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="telNum">行政区划</label>
                		<input type="hidden" name="adminDivCode" class="form-control" >
                		<input type="text" name="adminDivCodeName" class="form-control" >
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label for="telNum">联系电话</label>
                		<input type="text" name="telNum" class="form-control" >
                    </div>
                </div>
            </div>
            <div class="form-group">
                <a href="javascript:void(0)" id="orgSaveBt"  class="btn btn-primary pull-right">保存</a>
            </div>
    </form>
</div>
<script type="text/javascript">
    $(function () {
        var orgForm = $("#orgForm");
        var pid = '${param.pid}';
        var id = '${param.id}';
        orgForm.find("input[name=pid]").val(pid);
        orgForm.validation();
        if (id) {
            $.getJSON(ctx + '/orgAction/getOrg.do', {id: id}, function (data) {
                webUtils.fullForm({
                    form: orgForm,
                    data: data
                });
                
                if(data.adminDivCode){
                	var ds=webUtils.adminDivQuery(data.adminDivCode);
                	orgForm.find("input[name=adminDivCodeName]").val(ds[0].name);
                	orgForm.find("input[name=adminDivCode]").val(ds[0].code);
                }
            });
        }else{
        	if(pid){
        		$.getJSON(ctx + '/orgAction/getOrg.do', {id: pid}, function (data) {                    
                    if(data.adminDivCode){
                    	var ds=webUtils.adminDivQuery(data.adminDivCode);
                    	orgForm.find("input[name=adminDivCodeName]").val(ds[0].name);
                    	orgForm.find("input[name=adminDivCode]").val(ds[0].code);
                    }
                });
        	}
        }
        
        orgForm.find("input[name=adminDivCodeName]").click(function(){
        	var code=orgForm.find("input[name=adminDivCode]").val();
    		var name=orgForm.find("input[name=adminDivCodeName]").val();
        	webUtils.adminDivSelector({
    			hasSelectedAdminDivs:[{code:code,name:name}],
    			isSingle:true,
    			initAdminDivs:wiseMen.platform.initProperties.adminDiv,
    			onSelect:function(rows){
    				var names=[];
    				var codes=[];
    				$.each(rows,function(i,n){
    					names.push(n.name);
    					codes.push(n.code);
    				});
    				orgForm.find("input[name=adminDivCodeName]").val(names.join(","));
                	orgForm.find("input[name=adminDivCode]").val(codes.join(","));
    			}
    		});
        });
        
        $("#orgSaveBt").click(function () {
        	if(orgForm.valid()){
        		orgForm.ajaxSubmit({
                    url: ctx + '/orgAction/saveOrg.do',
                    data: {},
                    type: "POST",
                    success: function (data) {
                        if (data.success) {
                        	webUtils.alert("提交成功");
                            top.webUtils.topicPublish("org.edit.success", data);
                        } else {
                        	webUtils.error(data.msg);
                        }
                    }
                });
        	}
        });
    });
</script>